package com.wunong.smart.bigdata.flink.function;

import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.functions.co.RichCoFlatMapFunction;
import org.apache.flink.util.Collector;

/**
 * @author created by zealot.zt
 */
public class ExcludeSourceFunction extends RichCoFlatMapFunction<String, String, String> {

    /**
     * 是否存在标识
     */
    private ValueState<Boolean> blocked;

    @Override
    public void open(Configuration config) {
        ValueStateDescriptor<Boolean> descriptor = new ValueStateDescriptor<>("blocked", Boolean.class);
        blocked = getRuntimeContext().getState(descriptor);
    }

    @Override
    public void flatMap1(String controlValue, Collector<String> out) throws Exception {
        // 发起连接的流：若流1存在，则设置为true
        blocked.update(Boolean.TRUE);
    }

    @Override
    public void flatMap2(String dataValue, Collector<String> out) throws Exception {
        // 被连接的流：若流1中不存在，则添加
        if (blocked.value() == null) {
            out.collect(dataValue);
        }
    }

}